import Network from './network'
import network from "./network";

export const getBanner = () => Network.get('banner?type=2')
export const getPersonalized = () => Network.get('personalized?limit=6')
export const getNewAlbum = () => Network.get('album/newest')
export const getNewSong = () => Network.get('personalized/newsong')
export const getPlayList = (id) => Network.get('playlist/detail', id)
export const getAlbum = (id) => Network.get('album', id)
export const getSongDetail = (id) => Network.get('song/detail', id)
export const getSongLyric = (id) => Network.get('lyric', id)
export const getSongURL = (id) => Network.get('song/url', id)
export const getHotArtists = () => {
	return new Promise(function (resolve, reject) {
		Network.get('top/artists?offset=0&limit=5')
				.then(function (result) {
					resolve(result.artists)
				})
				.catch(function (err) {
					reject(err)
				})
	})
}
export const getLetterArtists = (letter) => {
	return new Promise(function (resolve, reject) {
		let letterArtists = []
		Network.all([
			network.get(`artist/list?offset=0&area=7&limit=5&type=-1&initial=${letter}`),
			network.get(`artist/list?offset=0&area=8&limit=5&type=-1&initial=${letter}`),
			network.get(`artist/list?offset=0&area=96&limit=5&type=-1&initial=${letter}`),
			network.get(`artist/list?offset=0&area=16&limit=5&type=-1&initial=${letter}`),
		])
				.then(function (result) {
					result.forEach(function (item) {
						letterArtists.push(...item.artists)
					})
					resolve(letterArtists);
				})
				.catch(function (err) {
					reject(err);
				})
	})

}
export const getAllArtists = () => {
	return new Promise(function (resolve, reject) {
		let keys = ['热']
		//第一个gethotartists是用于获取 热 字的,
		let list = [getHotArtists()]
		for (let i = 65; i <= 90; i++) {
			let char = String.fromCharCode(i)
			let lowerCase = char.toLowerCase();
			keys.push(char)
			list.push(getLetterArtists(lowerCase))
		}
		Network.all(list)
				.then(function (result) {
					let obj = {}
					obj.keys = keys
					obj.list = result
					resolve(obj);
				})
				.catch(function (err) {
					reject(err);
				})
	})
}
export const getArtistsSongs = (id) => Network.get('artists', id)
export const getSearchList = (data) => Network.get('search?type=1', data)
export const getHotSearch = () => Network.get('search/hot')
export const getTopListDetail = () => {
	return new Promise(function (resolve, reject) {
		let category = {
			officialList: [
				{ name: '飙升榜', id: 3 },
				{ name: '新歌榜', id: 0 },
				{ name: '原创榜', id: 2 },
				{ name: '热歌榜', id: 1 }
			],
			recList: [
				{ name: '云音乐说唱榜', id: 23 },
				{ name: '云音乐电音榜', id: 25 },
				{ name: '云音乐欧美热歌榜', id: 32 },
				{ name: '云音乐欧美新歌榜', id: 26 },
				{ name: '云音乐ACG榜', id: 22 },
				{ name: '云音乐古典榜', id: 24 }
			],
			globalList: [
				{ name: '美国Billboard榜', id: 6 },
				{ name: 'UK排行榜周榜', id: 5 },
				{ name: 'Beatport全球电子舞曲榜', id: 21 },
				{ name: '日本Oricon榜', id: 10 },
				{ name: 'iTunes榜', id: 8 },
				{ name: '云音乐日语榜', id: 29 }
			],
			otherList: [
				{ name: 'KTV唛榜', id: 7 },
				{ name: '法国 NRJ Vos Hits 周榜', id: 19 },
				{ name: '云音乐ACG动画榜', id: 27 },
				{ name: '云音乐韩语榜', id: 28 },
				{ name: '云音乐ACG游戏榜', id: 30 },
				{ name: '云音乐国电榜', id: 31 }
			],
			titles: { officialList: '官方榜', recList: '推荐榜', globalList: '全球榜', otherList: '更多榜单' }
		}
		Network.get('toplist/detail')
				.then(function (data) {
					data.list.forEach(function (obj) {
						let flag = false
						for (let key in category) {
							for (let i = 0; i < category[key].length; i++) {
								if (category[key][i].name === obj.name) {
									category[key][i].rank = obj
									flag = true
									break
								}
							}
							if (flag) {
								break
							}
						}
					})
					resolve(category)
				})
				.catch(function (err) {
					reject(err)
				})
	})
}
